<?php
require("../../../libraries/config.php");
require("../../../libraries/dbclass2.php");
require("../../../libraries/function.php");
require("../../../libraries/modules/manga/f1.php");

require("../../../libraries/cache.php");
//Xcrud remove event ----------------

//function check_anime_update($data,$primary,$xcrud){
	function manga_check_update($data,$primary,$xcrud){
	//Check user --------------
	$per=true;
	
	
	//Query get iformation
		$db=new db;
		$db->connect();
		$db->query("select upload_user,user_add,upload_user_add,theloai,hinhanh,image from anime where id=".intval($primary));
		$r1=$db->fetchOne();
		$db->close();
	
	if($_SESSION['user']['level']!=1&& $_SESSION['user']['level']!=2&& $_SESSION['user']['level']!=5){
			//Chỉ còn lại user level 4
		if($_SESSION['user']['username']!=$r1['upload_user']&&$_SESSION['user']['username']!=$r1['user_add']&& strpos($r1['upload_user_add'],$_SESSION['user']['username'])===false){
			$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Anime này!');
			$per=false;	
		}
	}
	//-------------------------
//	echo $data->get('theloai');
//The loai dau ma khac the loai truoc thi moi goi ham ra//
	if($per==true){
			if($r1['theloai']!=$data->get('theloai')){
				$db=new db;
				$db->connect();
				$db->nonquery("delete from anime_theloai_fk where anime_id=".$primary);	
				$db->close();
				mangaf1_manga_theloai($primary,$data->get('theloai'));
			}
				//Hình Ảnh------
			
				$image=$data->get('image');
				if($image!=$r1['image']){
					
					//Image trong db và image khi update khác nhau---------
					if($image!=''){
						$db=new db;
						$db->connect();
						$url=HOME."images/anime/".$image;
						$url=picasa_remote($url);
						
						$sql="update anime set hinhanh='".$db->security($url)."' where id=".intval($primary);
						$db->nonquery($sql);
					
						$db->close();
					}
			//End update image-------------------------------------
			}
			
			$data->set('alias',khongdau(trim($data->get('name'))));
			//Clear cache truyen
			$cache =new cache;
			$cache->flush("anime".$primary,'anime',1);
	}
	
}

//function check_anime_insert($data,$xcrud){
	function manga_check_insert($data,$xcrud){
   $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/
	$db=new db;
	$db->connect();
	$db->query("select id from anime where name = '".trim($data->get('name'))."'");
	if($db->num_rows()>0){
	 	$xcrud->set_exception('id','Manga này đã tồn tại rồi, xin vui lòng kiểm tra lại. Nếu  cần hỗ trợ xin vui lòng liên hệ ban quản trị Anime24h.vn!');
		$db->close();
	}else{
		$db->close();
		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
	//	$data->set('theloai',anime_theloai2($data->get('theloai')));
	}
	
	
}
function manga_check_afterinsert($data,$primary,$xcrud){
	
  // $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/

		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
		mangaf1_manga_theloai($primary,$data->get('theloai'));
		//Image nè
		$avatar=$data->get("image");
		if($avatar!=''){
		$url=HOME."images/anime/".$avatar;
		//Redirect Image Topicasa
		
		$url=picasa_remote($url);
	
				$db=new db;
				$db->connect();
				$sql="update anime set hinhanh='".$url."' where id=".$primary;
				$db->nonquery($sql);
				$db->close();
		
		//$db->nonquery("update users set avatar='".$url."' where id=".$primary);
	}
	
	
}



 //---------------------------------Anime 
  //function anime_remove($id){
	  
	 function manga_remove($id){ 	$cache =new cache;
	$cache->flush("anime".$id,'anime');
 	$db=new db;
	$db->connect();
	$db->nonquery("delete from chapterk2 where manga=".$id);
	$db->nonquery("delete from anime_theloai_fk where anime_id=".$id);
	$db->nonquery("delete from anime_tag3 where anime_id=".$id);
	$db->nonquery("delete from anime_tag2 where anime_id=".$id);
	$db->nonquery("delete from anime_tag1_fk where anime_id=".$id);
	$db->close();
 }
  
// function theloai_remove($id){
 function manga_theloai_remove($id){
 	$db=new db;
	$db->connect();
	$db->nonquery("delete from anime_theloai_fk where theloai_id=".$id);
	$db->nonquery("delete from anime_tag4 where theloai_id=".$id);
	$db->close();
 }
 
 
// function theloai_afterinsert($data,$primary,$xcrud){
	function manga_theloai_afterinsert($data,$primary,$xcrud){
  // $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/

		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
		anime_theloai($primary,$data->get('tags'));

	
	
}
// function theloai_update($data,$primary,$xcrud){
 function manga_theloai_update($data,$primary,$xcrud){
	
	//Check user --------------
	if($_SESSION['user']['level']!=1){
			//Chỉ còn lại user level 4
		$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Anime này!');
				
	}
	//-------------------------
//	echo $data->get('theloai');
//The loai dau ma khac the loai truoc thi moi goi ham ra//

	$db=new db;
	$db->connect();
	$db->query("select * from anime_theloai where id=".$primary);
	$r1=$db->fetchOne();
	if($r1['tags']!=trim($data->get('tags'))){
		//Clear tag cu da-------
		 $db->nonquery("delete from anime_tag4 where theloai_id=".$primary);
		 mangaf1_manga_theloai_tag($primary,trim($data->get('tags')));
	}
	

	
}

 




 //function episode_remove($id){
 function manga_episode_remove($id){
 	$db=new db;
	$db->connect();
	
	$db->nonquery("delete from anime_tag2 where ep_id=".$id);
	$db->query("select manga from chapterk2 where id=".$id);
	if($db->num_rows()>0){
		$r1=$db->fetchOne();
		$cache =new cache;
	$cache->flush("anime".$r1['manga'],'anime',1);
	}
	$db->close();
 }
 
 
 //function episode_update($data,$primary,$xcrud){
function manga_episode_update($data,$primary,$xcrud){	
	//Check user --------------
	if($_SESSION['user']['level']!=1&& $_SESSION['user']['level']!=2&& $_SESSION['user']['level']!=5){
			//Chỉ còn lại user level 4
		$user=$data->get('user_upload');
		if($_SESSION['user']['username']!=$user){
			$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Episode này!');
			
		}
	}
	$data->set('seourl',khongdau(trim($data->get('ten'))));
	//Clear tags
		$db=new db;
		$db->connect();
		$db->query("select ten from chapterk2 where id=".$primary);
		if($db->num_rows()>0){
		
			$r1=$db->fetchOne();
			if($r1['ten']!=$data->get('ten')){
				
				 //get anime name
				 $db->query("select name from anime where id=".$data->get('manga'));
				 if($db->num_rows()>0){
				
				 	$r2=$db->fetchOne();
					//-------Remove tag cu --------
					 $db->nonquery("delete from anime_tag2_fk where ep_id=".$primary);
					  $db->nonquery("update chapterk2 set gtag=0 where id=".$primary);
					 
				
					 
					
					//  $string=tag2_generate($r2['name'],trim($data->get('name')));
					//  echo $string;
					//  anime_tag2($primary,$data->get('fansub_id'),$data->get('anime_id'),$string);
				 }
				 // 
			}
		}
	//Check name không giống cũ thì phải clear tag //
	//-------------------------
//	echo $data->get('theloai');
//	anime_theloai($primary,$data->get('theloai'));
	//$data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("update truyensearch set ten='".mysql_real_escape_string($data->get('ten'))."',alias='".$data->get('alias')."' where id=".$primary);
   $db->close();*/
	
	//Clear cache anime
	$cache =new cache;
	$cache->flush("anime".$data->get('anime_id'),'anime',1);
	$sub=intval($primary)%100;
	$cache->flush('manga_get_chapter_detail'.$primary,'chapter/'.$sub);
	
	
}
 //---------------------------------End off anime function
 
 
 function manga_clear_slider(){
 $cache=new cache;
 $cache->flush("slide","home");
 }
?>
